package kisti.gui.CDF;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import daniel.kisti.Helper.Sam_Helper;



/**
 * 
 * <pre>
 * kisti.gui.CDF
 *   |_ SAM_DiskCacheMon.java
 *
 * </pre>
 *
 * Desc : sam disk 데이터를 패널에 출력
 * @Company : KISTI
 * @Author :daniel
 * @Date   :2011. 11. 14. 오후 5:21:37
 * @Version:
 *
 */
public class SAM_DiskCacheMon extends JPanel implements Runnable {
	private int nJob[] = {};
	private static int WIDTH = 1095;
	private static int HEIGHT = 676;
	private static int GRAPH_TOTAL_WIDTH = 300;
	private static int GRAPH_TOTAL_HEIGHT = 30;
	private static int STRING_X = 15;
	private static int GRAPH_X = 150;
	private static int GRAPH_TEXT_X = 500;

	private int samCDF01, samCDF02, cdfGeneral;
	private String samCDF01_percentage, samCDF02_percentage,
			cdfGeneral_percentage;
	private JTextArea taDstat;
	private Sam_Helper samHelper;

	/**
	 * 
	 * Desc :
	 * 
	 * @Method Name : JobSubmitionGraph
	 */
	public SAM_DiskCacheMon() {
		// TODO Auto-generated constructor stub

		this.setSize(WIDTH, HEIGHT);
		samHelper = new Sam_Helper();
		
		Thread t = new Thread(this);
		t.start();
	}

	public void run() {

		Random r = new Random();

		// while (true) {
		samHelper.loadSamDiskData();

		samCDF01_percentage = samHelper.getCdf01DiskUsage();
		samCDF02_percentage = samHelper.getCdf02DiskUsage();
		cdfGeneral_percentage = samHelper.getCdfUserDiskUsage();

		String cdf01 = samCDF01_percentage.replace("%", "");
		String cdf02 = samCDF02_percentage.replace("%", "");
		String user = cdfGeneral_percentage.replace("%", "");

		
		samCDF01 =  ((300/100)*Integer.parseInt(cdf01));
		samCDF02 =  ((300/100)*Integer.parseInt(cdf02));
		cdfGeneral =  ((300/100)*Integer.parseInt(user));

		repaint();

		// try {
		// Thread.sleep(1000);
		// } catch (Exception e) {}
		// }
	}

	/**
	 * paint
	 */
	public void paint(Graphics g) {

		g.clearRect(0, 0, getWidth(), getHeight());

		// SAM cache
		g.setColor(Color.black);
		g.drawString("SAM Station", 15, 20);// or 글자 이미지

		g.drawString("SAM cache CDF01", STRING_X, 70); // y += 40
		g.drawString("SAM cache CDF02", STRING_X, 110);
		g.drawString("CDF General Storage", STRING_X, 150);

		g.setColor(new Color(211, 211, 211));
		g.fillRect(GRAPH_X, 50, GRAPH_TOTAL_WIDTH * 2, GRAPH_TOTAL_HEIGHT);// BG
		g.fillRect(GRAPH_X, 90, GRAPH_TOTAL_WIDTH * 2, GRAPH_TOTAL_HEIGHT);// BG
		g.fillRect(GRAPH_X, 130, GRAPH_TOTAL_WIDTH * 2, GRAPH_TOTAL_HEIGHT);// BG

		g.setColor(new Color(0, 102, 255));
		g.fillRect(GRAPH_X, 50, samCDF01 * 2, GRAPH_TOTAL_HEIGHT);
		g.fillRect(GRAPH_X, 90, samCDF02 * 2, GRAPH_TOTAL_HEIGHT);
		g.fillRect(GRAPH_X, 130, cdfGeneral * 2, GRAPH_TOTAL_HEIGHT);

		// percentage
		g.setColor(Color.red);
		// g.drawString("-------------", 250, 10);
		g.drawString(samCDF01_percentage, 450, 70);
		g.drawString(samCDF02_percentage, 450, 110);
		g.drawString(cdfGeneral_percentage, 450, 150);

	}

	/**
	 * 
	 * Desc :
	 * 
	 * @Method Name : main
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame f = new JFrame();
		SAM_DiskCacheMon panel = new SAM_DiskCacheMon();
		// panel.setBackground(Color.YELLOW);
		f.setLayout(null);
		f.add(panel);
		f.setVisible(true);
		f.setSize(WIDTH, HEIGHT + 50);

		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}

			public void windowDeiconified(WindowEvent e) {
				System.out.println("windowDeiconified");
			}

			public void windowIconified(WindowEvent e) {
				System.out.println("windowIconified");
			}
		});

	}
}
